package com.ibeeking.found.msg.rest.controller.v1;

import com.ibeeking.found.msg.rest.service.IWxMpSourceService;
import com.ibeeking.found.msg.service.common.param.WxMpSourceParam;
import com.ibeeking.found.msg.service.common.query.WxMpImageTextQuery;
import com.ibeeking.found.msg.service.common.query.WxMpSourcePageQuery;
import com.ibeeking.found.msg.service.common.query.WxMpVideoQuery;
import com.ibeeking.found.msg.service.common.vo.WxMpSourcePageVO;
import com.ibeeking.found.msg.service.common.vo.WxMpSourceVO;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import com.ibeeking.nematos.log.annotation.Log;
import com.alibaba.nacos.common.http.param.MediaType;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @ClassName WxmpWxMenuController控制器
 * @Description
 * @Author ibeeking
 * @Date 2020-12-30 11:34
 **/
@RestController
@RequestMapping("/b/wxMp/source/v1")
public class WxMpSourceController {

    @Resource
    private IWxMpSourceService mpSourceServiceImpl;

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "素材分页列表")
    @PostMapping(value = "/pageList")
    public Page<WxMpSourcePageVO> pageList(@RequestBody @Validated WxMpSourcePageQuery query) {
        return mpSourceServiceImpl.pageList(query);
    }

    @Log(logType = OperateLogTypeEnum.SAVE, describe = "素材新增")
    @PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON)
    public String add(@RequestBody @Validated WxMpSourceParam param) {
        return mpSourceServiceImpl.add(param);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "素材修改")
    @PostMapping(value = "/modify")
    public Boolean modify(@RequestBody @Validated WxMpSourceParam param) {
        return mpSourceServiceImpl.modify(param);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "素材查看")
    @GetMapping(value = "/view/{id}")
    public WxMpSourceVO view(@PathVariable("id") Long id) {
        return mpSourceServiceImpl.view(id);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "素材查看")
    @GetMapping(value = "/viewByMediaId/{mediaId}")
    public WxMpSourceVO view(@PathVariable("mediaId") String mediaId) {
        return mpSourceServiceImpl.queryByMediaId(mediaId);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "素材发布")
    @GetMapping(value = "/publish/{id}")
    public Boolean publish(@PathVariable("id") Long id) {
        return mpSourceServiceImpl.publishMediaSource(id);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "素材删除")
    @GetMapping(value = "/del/{id}/{type}")
    public Boolean del(@PathVariable("id") Long id, @PathVariable("type") Integer type) {
        return mpSourceServiceImpl.delRemoteAndLocalSource(id, type);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "上传图文素材内，并返回地址")
    @PostMapping(value = "/uploadImageTextMediaToWx")
    public String uploadImageTextMediaToWx(@RequestBody @Validated WxMpImageTextQuery query) {
        return mpSourceServiceImpl.uploadImageTextMediaToWx(query);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "同步微信公众号图片素材")
    @PostMapping(value = "/syncPicSourceFromWx/{type}")
    public Integer syncPicSourceFromWx(@PathVariable("type") String type) {
        return mpSourceServiceImpl.syncPicSourceFromWx(type);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "下载可在线播放多媒体素材")
    @PostMapping(value = "/downloadPublishedVideoSource")
    public ResponseEntity<byte[]> downloadPublishedVideoSource(@RequestBody @Validated WxMpVideoQuery query) {
        return mpSourceServiceImpl.downloadPublishedVideoSource(query.getMediaId(), query.getFileName());
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "下载可在线播放多媒体素材")
    @PostMapping(value = "/downloadDraftVideoSource")
    public ResponseEntity<byte[]> downloadDraftVideoSource(@RequestBody @Validated WxMpVideoQuery query) {
        return mpSourceServiceImpl.downloadDraftVideoSource(query.getMediaId(), query.getFileName());
    }
}
